from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import redirect


class MyMiddleware(MiddlewareMixin):
    """中间件"""
    def process_request(self,request):
        # 排除不需要登录就可以访问的页面
        if request.path_info in ['/login/','/login/image/',"/login/pwd/",'/user/add/']:
            return

        # 获取session，若获取到session，则继续往后走
        info_dict = request.session.get("info")
        if info_dict:
            return

        # 若没有获取到session，返回登录页面
        return redirect('/login/')